<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="道具名称" prop="equips_Name">
        <el-input
          v-model="queryParams.equips_Name"
          placeholder="请输入道具名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="道具状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="道具状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:prop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="propList"
      row-key="equips_Id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="equips_Name" label="道具名称" :show-overflow-tooltip="true" width="160"></el-table-column>

      <el-table-column prop="equips_Img" label="图像" align="center" width="100">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.rowequips_Img && scope.rowequips_Img !== 'string' ? `${scope.rowequips_Img}` : `https://bpic.588ku.com/element_origin_min_pic/19/05/29/f6c79575c6ac60e37cfcfd774b1bddd4.jpg`"
            :fit="fit"></el-image>
        </template>
      </el-table-column>

      <el-table-column prop="price" label="价格" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.parentId === 0">&nbsp;</span>
          <span v-else>{{ scope.row.price }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="equips_Discount" label="道具折扣" :show-overflow-tooltip="true" width="160"></el-table-column>

      <el-table-column prop="canEquip" label="是否装备" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.canEquip">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>

      <el-table-column prop="isDelete" label="是否删除" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.isDelete">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>

      <el-table-column prop="equips_Position_Remart" label="位置" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.parentId === 0">&nbsp;</span>
          <span v-else>{{ scope.row.equips_Position_Remart }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="super" label="叠加" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.parentId === 0">&nbsp;</span>
          <span v-else>{{ scope.row.super ? `${scope.row.super}` : "否" }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="rarity" label="稀有度" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.parentId === 0">&nbsp;</span>
          <span v-else>{{ scope.row.rarity ? `${scope.row.rarity}` : 0 }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="code" label="二维码" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.parentId === 0">&nbsp;</span>
          <span v-else>{{ scope.row.code }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="equips_Type_Remart" label="道具大分类名称" width="160" :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="equips_Position_Remart" label="描述" :show-overflow-tooltip="true"></el-table-column>

      <el-table-column prop="status" label="状态" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="addTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="编辑时间" align="center" prop="editTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.editTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:prop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:prop:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:prop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改道具对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24" v-if="form.equips_Id">
            <el-form-item label="上级道具" prop="equips_Id">
              <treeselect
                v-model="form.equips_Id"
                :options="propOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择上级道具"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="道具大分类名称" prop="equips_Type_Id">
              <el-select v-model="form.equips_Type_Id" placeholder="道具大分类名称" clearable>
                <el-option
                  v-for="dict in equips_Type"
                  :key="dict.equips_Type_Id"
                  :label="dict.remart"
                  :value="dict.equips_Type_Id"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.equips_Type_Id === 1">
            <el-form-item label="道具图像" prop="icon">
              <el-upload
                class="upload-demo"
                name="file"
                data="{}"
                action="https://jsonplaceholder.typicode.com/posts/"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :file-list="fileList"
                list-type="picture">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="道具名称" prop="equips_Name">
              <el-input v-model="form.equips_Name" placeholder="请输入道具名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="form.equips_Type_Id != 3">
            <el-form-item label="道具价格" prop="price">
              <el-input-number v-model="form.price" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="form.equips_Type_Id != 3">
            <el-form-item label="道具折扣" prop="equips_Discount">
              <el-input-number v-model="form.equips_Discount" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="form.equips_Type_Id != 3">
            <el-form-item label="道具位置" prop="pos">
              <el-select v-model="form.equips_Position_Id" placeholder="菜单状态" clearable>
                <el-option
                  v-for="dict in equips_Position"
                  :key="dict.equips_Position_Id"
                  :label="dict.remark"
                  :value="dict.equips_Position_Id"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否装备" prop="canEquip">
              <el-radio-group v-model="form.canEquip">
                <el-radio
                  v-for="dict in canEquipArr"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否删除" prop="isDelete">
              <el-radio-group v-model="form.isDelete">
                <el-radio
                  v-for="dict in isDeleteArr"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item prop="status">
              <span slot="label">
                <el-tooltip content="选择停用则道具将禁用" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                道具状态
              </span>
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="form.equips_Type_Id != 3">
            <el-form-item label="叠加" prop="super">
              <el-input-number v-model="form.super" controls-position="right" :min="0" placeholder="请输入叠加" />
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="form.equips_Type_Id != 3">
            <el-form-item prop="rarity">
              <span slot="label">稀有度</span>
              <el-input-number v-model="form.rarity" controls-position="right" :min="0" placeholder="请输入稀有度" />
            </el-form-item>
          </el-col>

          <el-col :span="12" v-if="form.equips_Type_Id != 3">
            <el-form-item prop="code">
              <span slot="label">二维码</span>
              <el-image style="width: 100px; height: 100px" :src="form.code" :fit="fit" placeholder="请输入二维码" ></el-image>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item prop="remark">
              <span slot="label">描述</span>
              <el-input v-model="form.remark" type="textarea" :rows="5" placeholder="请输入描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProp, getProp, delProp, addProp, updateProp, getAllEquipsType, getEquipsPosition } from "@/api/system/prop";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

export default {
  name: "Prop",
  dicts: ['sys_show_hide', 'sys_normal_disable', 'sys_prop_pos'],
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 道具表格树数据
      propList: [],
      // 道具树选项
      propOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        CurrentPage: 1,
        PageSize: 10,
        equips_Name: undefined,
        isDelete: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        equips_Name: [
          { required: true, message: "道具名称不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "道具价格不能为空", trigger: "blur" }
        ],
        equips_Discount: [
          { required: true, message: "道具折扣不能为空", trigger: "blur" }
        ]
      },
      fit: "fill",     // ['fill', 'contain', 'cover', 'none', 'scale-down']
      canEquipArr: [
        {
          label: '是',
          value: true
        },
        {
          label: '否',
          value: false
        }
      ],
      isDeleteArr: [
        {
          label: '是',
          value: true
        },
        {
          label: '否',
          value: false
        }
      ],
      equips_Type: [],
      equips_Position: [],
      fileList: []
    };
  },
  created() {
    this.getList();
    this.getAllEquipsType();
    this.getEquipsPosition();
  },
  methods: {
    /* 获取全部的可穿戴部位 */
    getEquipsPosition(){
      getEquipsPosition().then(response => {
        this.equips_Position = response.data
      })
    },
    /* 获取全部的道具分类 */
    getAllEquipsType(){
      getAllEquipsType().then(response => {
        this.equips_Type = response.data
      })
    },
    /** 查询道具列表 */
    getList() {
      this.loading = true;
      listProp(this.queryParams).then(response => {
        this.propList = this.handleTree(response.data.item1, "equips_Id");
        this.loading = false;
      });
    },
    /** 转换道具数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.equips_Id,
        label: node.equips_Name,
        children: node.children
      };
    },
    /** 查询道具下拉树结构 */
    getTreeselect() {
      listProp().then(response => {
        this.propOptions = [];
        const prop = { equips_Id: 0, equips_Name: '主类目', children: [] };
        prop.children = this.handleTree(response.data, "equips_Id");
        this.propOptions.push(prop);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        equips_Id: undefined,
        equips_Type_Id: 1,
        equips_Name: undefined,
        equips_Discount: 0,
        equips_Img: 'https://bpic.588ku.com/element_origin_min_pic/19/05/29/f6c79575c6ac60e37cfcfd774b1bddd4.jpg',
        equips_Position_Id: 1,
        price: 0,
        canEquip: true,
        isDelete: false,

        parentId: 0,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.equips_Id) {
        this.form.parentId = row.equips_Id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加道具";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();

      for(let k in this.form) {
        this.form[k] = row[k]
      }
      this.open = true;
      this.title = "修改道具";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.equips_Id != undefined) {
            updateProp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.equips_Name + '"的数据项？').then(function() {
        return delProp(row.equips_Id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
  }
};
</script>
